<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserStoryController
 *
 * @author Julio Arredondo
 */
class UserStoryController extends CI_Controller{
    //put your code here

    public function show($id_back_log_item, $id_sprint = 0){ //muestra la interfaz principal

        $this->load->model("Sprint_model", "", true);
        $this->load->model("User_story_model", "", true);

        //obtener todos los user_stories
        $user_stories = $this->User_story_model->get_by_back_log($id_back_log_item);

        //empaquetando..
        $this->gui_data($user_stories,"user_stories");
        $this->gui_data( $id_back_log_item,"id_back_log_item");
        $this->gui_data($id_sprint,"id_sprint");

        //TODO: esto debe deducirse sin consulta
        if($this->Sprint_model->es_activo($id_sprint)){//se puede editar
            //cargar vista general
            $this->load->view("user_stories", $this->gui_data);
        }else{
            //cargar solo vista
            $this->load->view("user_stories_read", $this->gui_data);
        }
    }

    public function save($id_user_story, $id_back_log_item, $contenido){
        try{
            $contenido = urldecode($contenido);
            if($id_user_story == 0){
                $mensaje = $this->create($id_back_log_item,$contenido);
            }else{
                $mensaje = $this->update($id_back_log_item,$id_user_story, $contenido);
            }
            $exito = true;
        }catch(Exception $e){
            $exito = false;
            $mensaje = $e->getMessage();
        }

       $this->json_response($exito, $mensaje);
    }


    private function create($id, $data){
        try{
            $this->load->model("User_story_model", "", true);
            $id_user_story = $this->User_story_model->create($id, $data);
            return $id_user_story;
        }catch(Exception $e){
            throw $e;
        }
    }

    private function update($id_back_log_item, $id_user_story, $data){
        try{
            $this->load->model("User_story_model", "", true);
            $this->User_story_model->update($id_back_log_item, $id_user_story, $data);
        }catch(Exception $e){
            throw $e;
        }
    }

    public function delete($id){
        try{
            $this->load->model("User_story_model", "", true);
            $mensaje = $this->User_story_model->delete($id);
            $exito = true;
        }catch(Exception $e){
            $exito = false;
            $mensaje = $e->getMessage();
        }

       $this->json_response($exito, $mensaje);
    }


    public function generar_renglon_tabla($id, $contenido){
        $this->gui_data($id,"id_user_story");
        $this->gui_data($contenido,"contenido");
        $this->load->view('form_user_story_renglon', $this->gui_data);
    }

}
?>
